package com.scholarship.modules.student.service;

import com.scholarship.common.entity.RetResponse;
import com.scholarship.common.entity.RetResult;
import com.scholarship.modules.student.dto.FileUploadDto;
import com.upyun.RestManager;
import com.upyun.UpException;
import com.upyun.UpYunUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.xml.crypto.Data;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @author: huanghao
 * @date: 2021/4/22 20:43
 * @description:
 * @modifiedBy:
 */
/**
 * @author  zhz
 * @date  2021/6/14 19:33
 * 备注：修改了文件：上传文件到又拍云
 */
@Service
public class FileService {
    public RetResult saveFile(FileUploadDto fileDto){
        MultipartFile file = fileDto.getFile();
        String originalFilename = file.getOriginalFilename();
        String suffixName = originalFilename.substring(originalFilename.lastIndexOf(".")+1);

        String newFilename = UUID.randomUUID().toString() + "." +suffixName;
        //TODO 临时存储位置，而且后期要更改文件名加密及构成
        //新文件名构成：user_id + 加分项类型 + .后缀名
        //存储位置 服务器\\user_id的UID\\
        String filePath = "G:\\BNUZ\\";
        File dest = new File(filePath + newFilename);
        if (dest.getParentFile().exists() == false) {
            dest.getParentFile().mkdirs();
        }
        try{
            file.transferTo(dest);
        }
        catch (Exception e) {
            e.printStackTrace();
            return RetResponse.makeRsp(501, "服务器存储出错");
        }
        return RetResponse.makeOKRsp();
    }
}